package cn.hpclub.server.controller.admin;

import com.jfinal.aop.Before;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.activerecord.tx.Tx;

import cn.hpclub.server.bean.SystemConfig;
import cn.hpclub.server.model.OrgLocation;
import cn.hpclub.server.util.SystemConfigUtil;
import cn.hpclub.server.validator.admin.OrgLocationAddValidator;
import cn.hpclub.server.validator.admin.OrgLocationEditValidator;

/**
 * 后台类 - 地区管理
 * 
 */
public class OrgLocationController extends BaseAdminController<OrgLocation>{

    // 添加
    @Before({ Tx.class, OrgLocationAddValidator.class })
    public void add(){
        Record orgLocation = new Record();
        String name = getPara("name");
        String remark = getPara("remark");
        orgLocation.set("name", name).set("remark", remark).set("appid", 20442133);
        boolean success = Db.save("org_location", orgLocation);
        if(success){
            ajaxJsonSuccessJson(orgLocation);
        } else{
            ajaxJsonErrorMessage("新增失败");
        }
    }

    /*
     * 批量删除
     */
    @Before({ Tx.class })
    public void delete(){
        String ids = getPara("ids");
        Object[] para = new Object[] { "-1" };
        String[] array = new String[0];
        if(ids != null && !ids.isEmpty()){
            para = array = ids.split(",");
        }
        StringBuffer sb = new StringBuffer();
        sb.append("delete from org_location where id in(");
        if(array.length > 0){
            for(int i = 0; i < array.length; i++){
                if(i == array.length - 1){
                    sb.append("?");
                } else{
                    sb.append("?,");
                }
            }
            sb.append(")");
        } else{
            ajaxJsonErrorMessage("删除失败");
            return;
        }
        boolean success = Db.update(sb.toString(), para) > 0;
        if(success){
            ajaxJsonSuccessMessage("删除成功");
        } else{
            ajaxJsonErrorMessage("删除失败");
        }
    }

    @Before({ Tx.class })
    public void deleteById(){
        int id = getParaToInt("id");
        boolean success = Db.deleteById("org_location", id);
        if(success){
            ajaxJsonSuccessMessage("删除成功");
        } else{
            ajaxJsonErrorMessage("删除失败");
        }
    }

    // 编辑
    @Before({ Tx.class, OrgLocationEditValidator.class })
    public void edit(){
        int id = getParaToInt("id");
        String name = getPara("name");
        String remark = getPara("remark");
        Record orgLocation = Db.findById("org_location", id).set("appid", 20442133).set("name", name).set("remark",
                                                                                                          remark);
        boolean success = Db.update("org_location", orgLocation);
        if(success){
            ajaxJsonSuccessMessage("修改成功");
        } else{
            ajaxJsonErrorMessage("修改失败");
        }
    }

    // 列表
    public void list(){
        SystemConfig systemConfig = SystemConfigUtil.getSystemConfig();
        setAttr("systemConfig", systemConfig);
        setAttr("base", getRequest().getContextPath());
        int pageNumber = getParaToInt("pageNumber", 1);
        int pageSize = getParaToInt("pageSize", 10);
        Page<Record> pager = Db.paginate(pageNumber, pageSize, "select *", "from org_location");
        setAttr("orgLocationList", pager.getList());
        setAttr("totalPage", pager.getTotalPage());
        setAttr("pageSize", pager.getPageSize());
        setAttr("pageNumber", pager.getPageNumber());
        render("/admin/orglization_location_list.tpl");
    }

    /*
     * 分页
     */
    public void page(){
        int pageNumber = getParaToInt("pageNumber", 1);
        int pageSize = getParaToInt("pageSize", 10);
        Page<Record> pager = Db.paginate(pageNumber, pageSize, "select *", "from org_location");
        renderJson(pager);
    }

    public void search(){
        int pageNumber = getParaToInt("pageNumber", 1);
        int pageSize = getParaToInt("pageSize", 10);
        String name = getPara("name");
        Page<Record> pager = Db.paginate(pageNumber, pageSize, "select *", "from org_location where `name` like ?",
                                         "%" + name + "%");
        renderJson(pager);
    }
}
